/*
 * 常规活动设置
 * 乘客端邀请注册活动
 * @date 2016/5/9
 */


var passmtid = '';//乘客端邀请注册活动id
var passstate = '0';//乘客端邀请注册活动状态,默认关闭

//清空乘客端邀请注册活动详情
function clearPassShareInfo() {
    $("#passlinkwhenexpire").val('');
    $("#passlinkexpirein").val('');
    $("#passlinkexpiretime").val('');
    $("#passcnt").val('');
    $("#passsum").val('');
    $("#passbusiness input[type='checkbox']").prop("checked", false);
    $("#passcouwhenexpire").val('');
    $("#passcouexpirein").val('');
    $("#passcouexpiretime").val('');
    $("#passsubmitmark").text('');
}

//填充乘客端邀请注册活动
function fillPassShareInfo() {
    if (passmtid) {
        clearPassShareInfo();
    }
    $.ajax({
        type: "POST",
        url: "/activitysettings/gshareinfo",
        contentType: "application/json",
        dataType: "json",
        data: JSON.stringify({"type": "1"}),
        success: function (data) {
            if (data.status == 0) {
                if (data.mt) {
                    passmtid = data.mt.id;
                    passstate = data.state;
                    _fillPassShareInfo(data.mt);
                } else {
                    $("#passpayall").attr("checked", true);
                    $("#passclose").attr("checked", true);
                }
            } else if (data.status == -501) {//session过期
                window.location.href = '/timeout/index';
            } else {
                bbx_alert_error(data.message);
            }
        },
        error: function () {
            bbx_alert_error("出错啦~");
        }
    });
}

function _fillPassShareInfo(mt) {
    switch (mt.linkexpire) {//链接有效期
        case "0":
            $("#passlinkwhenexpire option[value='0']").prop("selected", true);
            break;
        case "1":
            $("#passlinkwhenexpire option[value='1']").prop("selected", true);
            $("#passlinkexpirein").val(mt.linkexpire_in);
            $("#passlinkexpireincss").show();
            break;
        case "2":
            $("#passlinkwhenexpire option[value='2']").prop("selected", true);
            $("#passlinkexpiretime").val(mt.linkexpire_time);
            $("#passlinkexpiretimecss").show();
            break;
    }
    $("#passcnt").val(mt.maxcount); //分享着最多可得数量
    $("#passsum").val(mt.sum); //金额
    var busi = Number(mt.usabletype); //业务
    if (busi == -1) {
        $("#passbusiness input[type='checkbox']").prop("checked", true);
    } else {
        var busiArray = new Array(0, 0, 0, 0);
        for (var i = 3; i >= 0; i--) {
            var j = parseInt(busi / (Math.pow(2, i)));
            if (j > 0) {
                busiArray[i] = 1;
            }
            busi %= (Math.pow(2, i));
            if (busi == 0) {
                break;
            }
        }
        if (busiArray[0]) {
            $("#passcitypin").prop("checked", true);
        }
        if (busiArray[1]) {
            $("#passcitybao").prop("checked", true);
        }
        if (busiArray[2]) {
            $("#passpackage").prop("checked", true);
        }
        if (busiArray[3]) {
            $("#passcity").prop("checked", true);
        }
    }
    switch (mt.paymode) {//优惠券支付方式
        case "1":
            $("#passpayonline").prop("checked", true);
            $("#passpayall").prop("checked", false);
            break;
        case "2":
            break;
        case "3":
            $("#passpayall").prop("checked", true);
            $("#passpayonline").prop("checked", false);
            break;
    }
    switch (mt.whenexpire) {//优惠券有效期
        case "0":
            $("#passcouwhenexpire option[value='0']").prop("selected", true);
            break;
        case "1":
            $("#passcouwhenexpire option[value='1']").prop("selected", true);
            $("#passcouexpirein").val(mt.expirein);
            $("#passcouexpireincss").show();
            break;
        case "2":
            $("#passcouwhenexpire option[value='2']").prop("selected", true);
            $("#passcouexpiretime").val(mt.expiretime);
            $("#passcouexpiretimecss").show();
            break;
    }
    if (passstate == '1') {
        $("#passopen").prop("checked", true);
        $("#passclose").prop("checked", false);
    } else {
        $("#passclose").prop("checked", true);
        $("#passopen").prop("checked", false);
    }
}

//点击乘客端邀请注册活动提交
$(document).delegate("#passsubmit", "click", function () {
    $("#passsubmitmark").text('');
    if (!checkPassShareParam()) {
        layer.msg('请重新填写选项!', {icon: 2, time: 1000/*1秒后自动关闭*/});
        return;
    }
    if (passmtid) {
        passModify();
    } else {
        passAdd();
    }
});

//检查乘客端邀请注册活动参数
function checkPassShareParam() {
    var rightParam = true;
    switch ($("#passlinkwhenexpire option:selected").val()) {
        case "1":
            $("#passlinkexpirein").val() ? $("#passlinkexpireinmark").text('') : ($("#passlinkexpireinmark").text('请填写！'), rightParam = false);
            break;
        case "2":
            $("#passlinkexpiretime").val() ? $("#passlinkexpiretimemark").text('') : ($("#passlinkexpiretimemark").text('请填写！'), rightParam = false);
            break;
    }
    $("#passcnt").val() ? $("#passcntmark").text('') : ($("#passcntmark").text('请填写！'), rightParam = false);
    $("#passsum").val() ? $("#passsummark").text('') : ($("#passsummark").text('请填写！'), rightParam = false);
    var allBusinessNotChecked = true;
    $("#passbusiness input").each(function () {
        if ($(this).prop("checked")) {
            allBusinessNotChecked = false;
        }
    });
    allBusinessNotChecked ? ($("#passbusinessmark").text('请选择业务类型！'), rightParam = false) : $("#passbusinessmark").text('');
    switch ($("#passcouwhenexpire option:selected").val()) {
        case "1":
            $("#passcouexpirein").val() ? $("#passcouexpireinmark").text('') : ($("#passcouexpireinmark").text('请填写！'), rightParam = false);
            break;
        case "2":
            $("#passcouexpiretime").val() ? $("#passcouexpiretimemark").text('') : ($("#passcouexpiretimemark").text('请填写！'), rightParam = false);
            break;
    }
    return rightParam;
}

//创建乘客端邀请注册活动参数
function createPassShareParam() {
    var business = 0;
    if ($("#passbusAll").prop("checked")) {
        business = -1;
    } else {
        $("#passbusiness input:checked").each(function () {
            business += parseInt($(this).attr('val'));
        });
    }
    var setState = 0;//是否设置状态  0:不设置；1：设置
    if (passstate != $("#passstate input:checked").attr('val')) {
        setState = 1;
    }
    var param = JSON.stringify({
        'mtid': passmtid,
        'acttype': "1",
        'actdescription': "新用户分享推广活动",
        'couponname': '分享各得10元',
        'coupondescript': '推荐好友下载并成功登陆帮邦行，各得5元',
        'sumstrategy': 1,
        'sum': Number($("#passsum").val()),
        'totalsum': 0,
        'totalcount': 0,
        'usabletype': business,
        'whenexpire': Number($("#passcouwhenexpire option:selected").val()),
        'expirein': Number($("#passcouexpirein").val()),
        'expiretime': $("#passcouexpiretime").val(),
        'order_paymode': -1,
        'paymode': Number($("#passcoupaymode input:checked").attr('val')),
        'getmethod': 2,
        'maxcount': Number($("#passcnt").val()),
        'linkexpire': Number($("#passlinkwhenexpire option:selected").val()),
        'linkexpire_in': Number($("#passlinkexpirein").val()),
        'linkexpire_time': $("#passlinkexpiretime").val(),
        'selfget': 0,
        'uniquescope': 1,
        'setstate': setState,
        'state': Number($("#passstate input:checked").attr('val'))
    });
    console.log(param);
    return param;
}
//添加乘客端邀请注册活动
function passAdd() {
    var param = createPassShareParam();
    var layerIndex = layer.msg('新增中。。。', {icon: 16, time: 3600000, shade: [0.5, '#fff']});
    $.ajax({
        type: "POST",
        url: "/activitysettings/ashare",
        contentType: "application/json",
        dataType: "json",
        data: param,
        success: function (data) {
            layer.close(layerIndex); //关闭加载层
            if (data.status == 0) {
                passstate = $("#passstate input:checked").attr('val');
                layer.msg('添加成功！', {icon: 1, time: 1000}, function () {
                    passmtid = data.id;
                    passstate = $("#passstate input:checked").attr('val');
                    layer.closeAll();
                });
            } else if (data.status == -501) {//session过期
                window.location.href = '/timeout/index';
            } else {
                bbx_alert_error(data.message);
            }
        },
        error: function () {
            layer.close(layerIndex); //关闭加载层
            bbx_alert_error("出错啦~");
        }
    });
}

//修改乘客端邀请注册活动
function passModify() {
    var param = createPassShareParam();
    var layerIndex = layer.msg('修改中。。。', {icon: 16, time: 3600000, shade: [0.5, '#fff']});
    $.ajax({
        type: "POST",
        url: "/activitysettings/mshare",
        contentType: "application/json",
        dataType: "json",
        data: param,
        success: function (data) {
            layer.close(layerIndex); //关闭加载层
            if (data.status == 0) {
                passstate = $("#passstate input:checked").attr('val');
                $("#passsubmitmark").text("√ 修改成功");
            } else if (data.status == -501) {//session过期
                window.location.href = '/timeout/index';
            } else {
                bbx_alert_error(data.message);
            }
        },
        error: function () {
            layer.close(layerIndex); //关闭加载层
            bbx_alert_error("出错啦~");
        }
    });
}